// core/include/storage.h
#ifndef STORAGE_H
#define STORAGE_H

#include "storage_types.h"
#include "storage_config.h"
#include "esp_littlefs.h"

#ifdef __cplusplus
extern "C" {
#endif

/* 基础文件系统操作 */
esp_err_t storage_init(storage_context_t* ctx, const storage_config_t* config);
esp_err_t storage_deinit(storage_context_t* ctx);
esp_err_t storage_format(storage_context_t* ctx);

/* 文件操作接口 */
esp_err_t storage_read(storage_context_t* ctx, const char* path, void* data, size_t size, size_t* read_size);
esp_err_t storage_write(storage_context_t* ctx, const char* path, const void* data, size_t size);
esp_err_t storage_append(storage_context_t* ctx, const char* path, const void* data, size_t size);
esp_err_t storage_remove(storage_context_t* ctx, const char* path);
esp_err_t storage_rename(storage_context_t* ctx, const char* old_path, const char* new_path);

/* 目录操作接口 */
esp_err_t storage_mkdir(storage_context_t* ctx, const char* path);
esp_err_t storage_rmdir(storage_context_t* ctx, const char* path);
esp_err_t storage_list_dir(storage_context_t* ctx, const char* path, storage_dir_entry_t* entries, size_t* num_entries);

/* 文件信息接口 */
esp_err_t storage_stat(storage_context_t* ctx, const char* path, storage_file_info_t* info);
esp_err_t storage_exists(storage_context_t* ctx, const char* path, bool* exists);
esp_err_t storage_get_info(storage_context_t* ctx, storage_info_t* info);

/* 设备管理接口 */
esp_err_t storage_mount_device(storage_context_t* ctx, const storage_device_config_t* config);
esp_err_t storage_unmount_device(storage_context_t* ctx);

// 扩展功能接口
#ifdef CONFIG_STORAGE_ENABLE_EXTENSION
/* 缓存操作接口 */
esp_err_t storage_cache_enable(storage_context_t* ctx, bool enable);
esp_err_t storage_cache_invalidate(storage_context_t* ctx);
esp_err_t storage_cache_flush(storage_context_t* ctx);

/* DMA操作接口 */
esp_err_t storage_dma_enable(storage_context_t* ctx, bool enable);
esp_err_t storage_dma_configure(storage_context_t* ctx, const void* config);

/* 数据保护接口 */
esp_err_t storage_protection_enable(storage_context_t* ctx, bool enable);
esp_err_t storage_check_integrity(storage_context_t* ctx);
esp_err_t storage_repair(storage_context_t* ctx);

/* 性能监控接口 */
esp_err_t storage_get_stats(storage_context_t* ctx, storage_stats_t* stats);
esp_err_t storage_reset_stats(storage_context_t* ctx);
#endif // CONFIG_STORAGE_ENABLE_EXTENSION

#ifdef __cplusplus
}
#endif

#endif /* STORAGE_H */




// include/storage_types.h
#ifndef STORAGE_TYPES_H
#define STORAGE_TYPES_H

#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "esp_err.h"

// 基础配置常量
#define STORAGE_MAX_PATH_LEN CONFIG_LITTLEFS_MAX_NAME_LENGTH
#define STORAGE_MAX_FILES CONFIG_LITTLEFS_MAX_FILES
#define STORAGE_MAX_OPEN_FILES CONFIG_LITTLEFS_MAX_OPEN_FILES


// 错误码定义
#define STORAGE_ERR_BASE                0x1000  
#define STORAGE_ERR_INVALID_STATE       (STORAGE_ERR_BASE + 1)
#define STORAGE_ERR_DEVICE_FAIL         (STORAGE_ERR_BASE + 2)
#define STORAGE_ERR_NOT_MOUNTED         (STORAGE_ERR_BASE + 3)
#define STORAGE_ERR_PATH_TOO_LONG       (STORAGE_ERR_BASE + 4)
#define STORAGE_ERR_NOT_FOUND           (STORAGE_ERR_BASE + 5)

// 文件系统状态
typedef enum {
    STORAGE_STATE_UNINITIALIZED = 0,
    STORAGE_STATE_MOUNTED,
    STORAGE_STATE_UNMOUNTED,
    STORAGE_STATE_ERROR 
} storage_state_t;

// 文件类型
typedef enum {
    STORAGE_TYPE_REGULAR = 1,
    STORAGE_TYPE_DIRECTORY,
    STORAGE_TYPE_SYMLINK
} storage_file_type_t;

// 设备类型
typedef enum {
    STORAGE_DEVICE_FLASH = 0,
    STORAGE_DEVICE_SD,
    STORAGE_DEVICE_MAX
} storage_device_type_t;

// 文件信息结构
typedef struct {
    char name[STORAGE_MAX_PATH_LEN];
    size_t size;
    storage_file_type_t type;
    time_t create_time;
    time_t modify_time;
    struct {
        bool is_hidden;
        bool is_readonly;
        uint32_t flags;
    } attributes;
} storage_file_info_t;

// 目录条目结构
typedef struct {
    storage_file_info_t info;
    void *handle;
} storage_dir_entry_t;

// 设备配置结构
typedef struct {
    storage_device_type_t type;
    union {
        struct {
            const char *partition_label;
        } flash_config;
        struct {
            int miso_pin;
            int mosi_pin;
            int sck_pin;
            int cs_pin;
        } sd_config;
    };
} storage_device_config_t;

// 文件系统扩展功能配置
typedef struct {
    struct {
        bool enable_cache;
        size_t cache_size;
        bool enable_prefetch;
    } cache_config;

    struct {
        bool enable_dma;
        size_t buffer_size;
        uint8_t buffer_count;
    } dma_config;

    struct {
        bool enable_protection;
        bool auto_recover;
    } protection_config;
} storage_extension_config_t;

// 存储配置结构
typedef struct {
    char base_path[32];
    char partition_label[16];
    bool format_if_mount_failed;
    storage_device_config_t device;
    storage_extension_config_t extension;
} storage_config_t;

// 存储信息结构
typedef struct {
    size_t total_bytes;
    size_t used_bytes; 
    size_t free_bytes;
    struct {
        size_t block_size;
        size_t block_count;
        size_t sector_size;
    } geometry;
    struct {
        uint32_t total_files;
        uint32_t open_files;
        uint32_t max_name_len;
    } limits;
    struct {
        bool is_mounted;
        bool is_readonly;
        bool needs_repair;
    } status;
} storage_info_t;

// 存储统计信息
typedef struct {
    struct {
        uint32_t reads;
        uint32_t writes;
        uint32_t deletes;
        uint64_t read_bytes;
        uint64_t write_bytes;
    } operations;
    struct {
        uint32_t cache_hits;
        uint32_t cache_misses;
        uint32_t dma_transfers;
    } performance;
    struct {
        uint32_t errors;
        uint32_t recoveries;
    } reliability;
} storage_stats_t;

// 存储上下文结构
typedef struct {
    storage_config_t config;
    storage_info_t info;
    storage_stats_t stats;
    storage_state_t state;
    SemaphoreHandle_t mutex;
    struct {
        void *cache_ctx;
        void *dma_ctx;
        void *protection_ctx;
    } extension;
} storage_context_t;

#endif /* STORAGE_TYPES_H */


// include/storage_config.h
#ifndef STORAGE_CONFIG_H 
#define STORAGE_CONFIG_H

#include "storage_types.h"

// Flash设备默认配置
#define STORAGE_FLASH_DEFAULT_CONFIG() { \
    .type = STORAGE_DEVICE_FLASH, \
    .flash_config = { \
        .partition_label = "storage" \
    } \
}

// SD卡默认配置
#define STORAGE_SD_DEFAULT_CONFIG() { \
    .type = STORAGE_DEVICE_SD, \
    .sd_config = { \
        .miso_pin = CONFIG_STORAGE_SD_MISO, \
        .mosi_pin = CONFIG_STORAGE_SD_MOSI, \
        .sck_pin = CONFIG_STORAGE_SD_SCK, \
        .cs_pin = CONFIG_STORAGE_SD_CS \
    } \
}

// 扩展功能默认配置
#define STORAGE_EXTENSION_DEFAULT_CONFIG() { \
    .cache_config = { \
        .enable_cache = true, \
        .cache_size = 32 * 1024, \
        .enable_prefetch = true \
    }, \
    .dma_config = { \
        .enable_dma = true, \
        .buffer_size = 4 * 1024, \
        .buffer_count = 2 \
    }, \
    .protection_config = { \
        .enable_protection = true, \
        .auto_recover = true \
    } \
}

// 存储系统默认配置
#define STORAGE_DEFAULT_CONFIG() { \
    .base_path = "/storage", \
    .partition_label = "storage", \
    .format_if_mount_failed = true, \
    .device = STORAGE_FLASH_DEFAULT_CONFIG(), \
    .extension = STORAGE_EXTENSION_DEFAULT_CONFIG() \
}

#endif /* STORAGE_CONFIG_H */